<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Page Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
    <div>a：<span id="main"></span></div>
    <iframe src="http://localhost:4000/b.html" id="frame" onload="load()" frameborder="0"></iframe>
    <script>
        /* 
        h5的postMessage()方法可以安全地实现跨源通信
        需求是http://127.0.0.1:3000/a.html 想对http://127.0.0.1:4000/b.html 说我爱你,之后b页面收到消息后对其说我不爱你~,
        */
        function load(){
            let frame=document.getElementById('frame');
            frame.contentWindow.postMessage('我爱你','http://localhost:4000');
            window.onmessage=function(e){
                document.querySelector('#main').textContent=e.data;
                console.log(e.data);
            }
        }
    </script>
</body>
</html>